var APIClient = require('../../../api-client/');

var client = new APIClient({
  appKey: 'a10086',
  appSecret: 'xffcncgmveu6slxg',
  callbackUrl: 'http://127.0.0.1:3000/example/auth/callback'
})

// 跳转获取授权
exports.requestAuth = function (req, res, next) {
  res.redirect(client.getRedirectUrl());
};


exports.example = function (req, res, next) {
  // 如果未获得授权码，则先跳转到授权页面
  if(!client._accessToken) return res.redirect('/example/auth');

  // 请求获取文章列表
  client.getArticles(req.query, function(err, ret){
    if(err) return res.json(err);
    res.send({
      accessToken: client._accessToken,
      result: ret
    })
  });
};

exports.authCallback = function (req, res, next) {
  client.requestAccessToken(req.query.code, function(err, ret){
    if (err) return res.send(err.toString());

    res.redirect('/example');
  });
};